package com.jy.api.payservice;

import com.alibaba.fastjson.JSON;
import com.jy.api.Constants.ExtConstants;
import com.jy.api.payReq.TransBaseRequest;
import com.jy.api.payRes.TransBaseResponse;
import com.jy.api.payUtil.ErrorConstants;
import com.jy.api.payUtil.JlpayException;


/**
 * @author zhaoyang2
 * 交易执行类
 */
public class TransExecuteService {

	public TransExecuteService() {
	}

	public static <T extends TransBaseResponse> T executor(TransBaseRequest request, Class<? extends TransBaseResponse> clazz) {
		String sysPriKey = System.getProperty("qrcode.org.privatekey");
		String jlPubKey = System.getProperty("qrocde.jlpay.publickey");
		String tradeUrl = System.getProperty("qrcode.jlpay.tradeUrl");
		if (null != sysPriKey && null != jlPubKey && null != tradeUrl) {
			String requestStr = CommonService.sign(request, sysPriKey);

			String responseStr = CommonService.httpToInvoke(requestStr, tradeUrl);
			CommonService.checkSign(responseStr, jlPubKey);
			return (T)JSON.parseObject(responseStr, clazz);
		} else {
			throw new JlpayException(ErrorConstants.VALIDATE_ERROR_1, "请设置系统参数");
		}
	}
}
